package demo150;

/*

class Process {
    int pid;
    int arrivalTime;
    int burstTime;
    int priority; // 优先级（用于优先级调度算法）

    public Process(int pid, int arrivalTime, int burstTime, int priority) {
        this.pid = pid;
        this.arrivalTime = arrivalTime;
        this.burstTime = burstTime;
        this.priority = priority;
    }
}

interface Scheduler {
    void schedule(List<Process> processes);
}

// 先来先服务（FCFS）调度算法的实现
class FCFSScheduler implements Scheduler {
    @Override
    public void schedule(List<Process> processes) {
        // 根据到达时间对进程进行排序
        processes.sort(Comparator.comparingInt(p -> p.arrivalTime));

        int currentTime = 0;
        for (Process process : processes) {
            // 如果当前时间小于进程的到达时间，则更新当前时间为进程的到达时间
            if (currentTime < process.arrivalTime) {
                currentTime = process.arrivalTime;
            }

            System.out.println("Process " + process.pid + " starts at " + currentTime + " and ends at " + (currentTime + process.burstTime));
            currentTime += process.burstTime; // 更新当前时间为进程的结束时间
        }
    }
}

// 短作业优先（SJF）调度算法的实现(非抢占式SJF)
class SJFScheduler implements Scheduler {
    @Override
    public void schedule(List<Process> processes) {
        // 根据执行时间对进程进行排序
        processes.sort(Comparator.comparingInt(p -> p.burstTime));

        int currentTime = 0;
        for (Process process : processes) {
            // 如果当前时间小于进程的到达时间，则更新当前时间为进程的到达时间
            if (currentTime < process.arrivalTime) {
                currentTime = process.arrivalTime;
            }

            System.out.println("Process " + process.pid + " starts at " + currentTime + " and ends at " + (currentTime + process.burstTime));
            currentTime += process.burstTime; // 更新当前时间为进程的结束时间
        }
    }
}

// 时间片轮转（RR）调度算法的实现
class RoundRobinScheduler implements Scheduler {
    private int timeQuantum; // 时间片长度

    public RoundRobinScheduler(int timeQuantum) {
        this.timeQuantum = timeQuantum;
    }

    @Override
    public void schedule(List<Process> processes) {
        Queue<Process> queue = new LinkedList<>(processes);
        int currentTime = 0;

        while (!queue.isEmpty()) {
            Process process = queue.poll();
            // 如果当前时间小于进程的到达时间，则更新当前时间为进程的到达时间
            if (currentTime < process.arrivalTime) {
                currentTime = process.arrivalTime;
            }
            // 计算实际运行的时间片长度，取时间片和剩余执行时间的最小值
            int executionTime = Math.min(timeQuantum, process.burstTime);
            // 输出进程的开始时间和运行的时间片长度
            System.out.println("Process " + process.pid + " starts at " + currentTime + " and runs for " + executionTime + " units");
            currentTime += executionTime; // 更新当前时间为进程的结束时间
            process.burstTime -= executionTime; // 减少进程的剩余执行时间
            if (process.burstTime > 0) {
                System.out.println("Process"+process.pid+" complete "+executionTime+" and Process still need "+process.burstTime);//打印进程进度及剩余执行时间
                queue.add(process); // 如果进程未完成，则重新加入队列尾部
            } else {
                System.out.println("Process " + process.pid + " finishes at " + currentTime); // 输出进程的完成时间
            }
        }
    }
}

// 优先级调度算法的实现
class PriorityScheduler implements Scheduler {
    @Override
    public void schedule(List<Process> processes) {
        // 根据优先级对进程进行排序（优先级数值越小，优先级越高）
        processes.sort(Comparator.comparingInt(p -> p.priority));

        int currentTime = 0;
        for (Process process : processes) {
            // 如果当前时间小于进程的到达时间，则更新当前时间为进程的到达时间
            if (currentTime < process.arrivalTime) {
                currentTime = process.arrivalTime;
            }

            int endtime = currentTime + process.burstTime;
            System.out.println("Process " + process.pid + " starts at " + currentTime + " and ends at " + endtime);
            currentTime += process.burstTime; // 更新当前时间为进程的结束时间
        }
    }
}

public class ProcessScheduler {
    public static void main(String[] args) {

        List<Process> processes = ProcessScheduler.init();

        // 先来先服务调度算法
        Scheduler scheduler1 = new FCFSScheduler();

        // 短作业优先调度算法
        Scheduler scheduler2 = new SJFScheduler();

        // 时间片轮转调度算法
        System.out.print("Enter time quantum: "); // 提示用户输入时间片长度
        Scanner scanner = new Scanner(System.in);
        int timeQuantum = scanner.nextInt(); // 读取用户输入的时间片长度
        Scheduler scheduler3 = new RoundRobinScheduler(timeQuantum);

        // 优先级调度算法
        Scheduler scheduler4 = new PriorityScheduler();


        System.out.println("先来先服务调度算法:");
        scheduler1.schedule(processes);

        processes = ProcessScheduler.init();
        System.out.println("短作业优先调度算法:");
        scheduler2.schedule(processes);

        processes = ProcessScheduler.init();
        System.out.println("时间片轮转调度算法:");
        scheduler3.schedule(processes);

        processes = ProcessScheduler.init();
        System.out.println("优先级调度算法:");
        scheduler4.schedule(processes);
    }

    public static List<Process> init() {
         List<Process> proc = Arrays.asList(
                // PID=1, 到达时间=0, 执行时间=5, 优先级=2
                new Process(1, 0, 5, 2),
                // PID=2, 到达时间=1, 执行时间=3, 优先级=1
                new Process(2, 1, 3, 1),
                // PID=3, 到达时间=2, 执行时间=8, 优先级=3
                new Process(3, 2, 8, 3),
                // PID=4, 到达时间=3, 执行时间=6, 优先级=5
                new Process(4, 3, 6, 5),
                // PID=5, 到达时间=12, 执行时间=3, 优先级=4
                new Process(5, 12, 3, 4)
        );
         return proc;
    }
}

*/
